import * as types from '../types'
//默认1：上海路灯系统
let tencent = window.location.hash.split('/')[3]||sessionStorage.getItem('tencentId') || 1
const state = {
  path:'',//记录路径
  clientWidth:document.body.clientWidth-180,
  clientHeight:document.body.clientHeight-100,
  sysList:JSON.parse(localStorage.getItem('sysList'+tencent))||[],
  currSys:JSON.parse(localStorage.getItem('currSys'+tencent))||{},
  serverURL: window.global.webServerURL+':9404'
}
const actions = {
}

const getters = {
  path: state => state.path,
  sysList: state => state.sysList,
  currSys: state => state.currSys,
  clientWidth:state=>state.clientWidth,
  clientHeight:state=>state.clientHeight,
  serverURL:state=>state.serverURL,
}
const mutations = {
  [types.LAST_PATH](state,v){
    state.path=v
  },
  setNavActive(state,v){
    state.navActive=v
  },
  setSysList(state,v){
    let list=state.sysList
    let hash = {}
    let newArr=[]
    if(!v.isDelete){//添加
      list = [...list,v]
      newArr = list.reduceRight((item, next) => {
        hash[next.value] ? '' : hash[next.value] = true && item.push(next)
        return item
      }, []);

    }else {//删除
      newArr=list.filter((item, index) => item.value != v.value)
      if(!newArr.length){
        this.commit('comm/setCurrSystem',{})
      }
    }
    localStorage.setItem('sysList'+tencent,JSON.stringify(newArr))
    state.sysList=newArr
  },
  setCurrSystem(state,v){
    localStorage.setItem('currSys'+tencent,JSON.stringify(v))
    state.currSys=v
  },
  setServerURL(state,v){
    state.serverURL=v
  }
}

export default {
  namespaced:true,
  state,
  getters,
  actions,
  mutations,

}
